﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_Modules_khachhang_AddNew : System.Web.UI.UserControl
{
    WebData db = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnAddNew.Attributes.Add("onclick", "return OnSubmitClick();");
        this.btnCancel.Attributes.Add("onclick","return ClearAll()");

        if (!this.IsPostBack) {
            db = new WebData();
            LoadCombobox();
            ViewState["URLReferrer"] = Request.UrlReferrer.AbsoluteUri;
        }
    }

   

    private void LoadCombobox() {
        if (db == null) {
            db = new WebData();
        }
        cbbidGroup.DataSource = db.tbl_Group_Khachhangs.GetAll();
        cbbidGroup.DataTextField = "nameGroup";
        cbbidGroup.DataValueField = "idGroup";
        cbbidGroup.DataBind();
    }

    private void AddNew() {
        String strMess = "";
        int flag = -1;
        if (db == null)
        {
            db = new WebData();
        }
            try
            {
                
                DataLibrary.tbl_Khachhang item = new DataLibrary.tbl_Khachhang();
                item.NameKhachhang = txtnameKhachhang.Text;
                item.IdGroup = Convert.ToInt32(cbbidGroup.SelectedValue);
                flag = db.tbl_Khachhangs.InsertOnSubmit(item);
            }
            catch (Exception err)
            {
                strMess = Message.Insert_err + "! vì lỗi " + err.Message.ToString();
            }


            if (flag != -1)
            Response.Redirect(ViewState["URLReferrer"].ToString());
        else
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + Message.Insert_err + "');", true);
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        AddNew();
    }
}